package com.ruoyi.project.digital.service;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;

import com.ruoyi.project.digital.config.properties.SysUrlProperties;
import com.ruoyi.project.digital.domain.Apply;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.util.List;

/**
 * @program: zxht
 * @Date: 2020/12/17 15:54
 * @Author: syc
 * @Description:公众号模板消息
 */
@Component
public class TemplateMsgService {
    @Autowired
    private WxMpService wxMpService;
    @Autowired
    private SysUrlProperties sysUrlProperties;
    //申请成功
    private static String apply_success_template_id ="A1cdKf4NsJEuNMB9taJHDyz94PJiLi9jLgfuB_GnFd4";
    //审核结果
    private static String audit_result_template_id ="3eMiWt4dUQCheGXWmENFYLYaTwl-sZD9aGf19CAHmv4";
    //待支付
    private static String wait_pay_template_id ="eeoZW_CccOyJlUe4yTWeL0P3FjpAHwwLX4exOxyFhRk";
    //已支付
    private static String finished_pay_template_id ="z-_raY3xx0f2rmGs_TO7SVsTsl1O077mPLu9h0bLkqw";
    //已邮寄
    private static String mailed_template_id ="YTOeubwi2U8MOM3hxLBZPFfkKoSYWkr9-QfOYIjaU4I";

    /**
     * 申请成功通知
     */
    public void applySuccessMsg(String openId, Apply apply){
        WxMpTemplateMessage tm = new WxMpTemplateMessage();
        tm.setToUser(openId);
        tm.setUrl(sysUrlProperties.getWebUrl()+apply.getId());
        tm.setTemplateId(apply_success_template_id);
        List<WxMpTemplateData> list = Lists.newArrayList();
        list.add(new WxMpTemplateData("first","申请成功通知",null));
        list.add(new WxMpTemplateData("keyword1",apply.getName(),null));
        list.add(new WxMpTemplateData("keyword2",apply.getProvince()+apply.getCity()+apply.getCounty()+apply.getAddressDetails(),null));
        list.add(new WxMpTemplateData("keyword3",apply.getReceiveName(),null));
        list.add(new WxMpTemplateData("keyword4",apply.getReceiveMobile(),null));
        list.add(new WxMpTemplateData("keyword5", DateUtil.offsetMonth(apply.getCreateTime(),1).toString(),null));
        list.add(new WxMpTemplateData("remark",null,null));
        tm.setData(list);
        sendTemplateMsg(tm);
    }

    /**
     * 审核结果通知
     */
    public void auditResultMsg(Long id,String openId,boolean flag,String auditName){
        WxMpTemplateMessage tm = new WxMpTemplateMessage();
        tm.setToUser(openId);
        tm.setUrl(sysUrlProperties.getWebUrl()+id);
        tm.setTemplateId(audit_result_template_id);
        List<WxMpTemplateData> list = Lists.newArrayList();
        list.add(new WxMpTemplateData("first",flag?"您的申请已通过审核！":"您的申请未通过审核！",null));
        list.add(new WxMpTemplateData("keyword1",auditName,null));
        list.add(new WxMpTemplateData("keyword2","打印病历申请",null));
        list.add(new WxMpTemplateData("keyword3",DateUtil.now(),null));
        list.add(new WxMpTemplateData("remark",null,null));
        tm.setData(list);
        sendTemplateMsg(tm);
    }

    /**
     * 待支付通知
     */
    public void waitPayMsg(Long id,String openId, String applyNo, BigDecimal amount,String createTime){
        WxMpTemplateMessage tm = new WxMpTemplateMessage();
        tm.setToUser(openId);
        tm.setUrl(sysUrlProperties.getWebUrl()+id);
        tm.setTemplateId(wait_pay_template_id);
        List<WxMpTemplateData> list = Lists.newArrayList();
        list.add(new WxMpTemplateData("first","您有待支付信息",null));
        list.add(new WxMpTemplateData("keyword1",applyNo,null));
        list.add(new WxMpTemplateData("keyword2",amount+"元",null));
        list.add(new WxMpTemplateData("keyword3",createTime,null));
        list.add(new WxMpTemplateData("remark",null,null));
        tm.setData(list);
        sendTemplateMsg(tm);
    }

    /**
     * 支付通知
     */
    public void finidedPayMsg(Long id ,String openId, String applyNo,String name, BigDecimal amount,String createTime){
        WxMpTemplateMessage tm = new WxMpTemplateMessage();
        tm.setToUser(openId);
        tm.setUrl(sysUrlProperties.getWebUrl()+id);
        tm.setTemplateId(finished_pay_template_id);
        List<WxMpTemplateData> list = Lists.newArrayList();
        list.add(new WxMpTemplateData("first","支付成功",null));
        list.add(new WxMpTemplateData("keyword1",applyNo,null));
        list.add(new WxMpTemplateData("keyword2","打印申请",null));
        list.add(new WxMpTemplateData("keyword3",name,null));
        list.add(new WxMpTemplateData("keyword4",amount+"元",null));
        list.add(new WxMpTemplateData("keyword5",createTime,null));
        list.add(new WxMpTemplateData("remark",null,null));
        tm.setData(list);
        sendTemplateMsg(tm);
    }

    /**
     * 支付通知
     */
    public void mailedMsg(Long id,String openId, String expressNo){
        WxMpTemplateMessage tm = new WxMpTemplateMessage();
        tm.setToUser(openId);
        tm.setUrl(sysUrlProperties.getWebUrl()+id);
        tm.setTemplateId(mailed_template_id);
        List<WxMpTemplateData> list = Lists.newArrayList();
        list.add(new WxMpTemplateData("first","您有一个运单已经发出",null));
        list.add(new WxMpTemplateData("keyword1","快递公司",null));
        list.add(new WxMpTemplateData("keyword2",expressNo,null));
        list.add(new WxMpTemplateData("remark",null,null));
        tm.setData(list);
        sendTemplateMsg(tm);
    }


    public void sendTemplateMsg(WxMpTemplateMessage wmtm){
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(wmtm);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }
}
